package org.app.Utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.app.Entity.Admin;
import org.app.Entity.User;

import java.util.Date;

public class JwtUtil {
    private static final long EXPIRE = 60 * 60 * 24 * 7 * 1000L;
    private static final String SECRET = "!ad#12~";

    public static String getUserToken(User user) {
        JWTCreator.Builder builder = JWT.create();
        return builder.withClaim("phone", user.getC_phone())
                .withClaim("name",user.getC_name())
                .withClaim("c_id",user.getC_id())
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE))
                .sign(Algorithm.HMAC256(SECRET));
    }
    public static String getAdminToken(Admin admin) {
        JWTCreator.Builder builder = JWT.create();
        return builder.withClaim("phone", admin.getAdmin_phone())
                .withClaim("name",admin.getAdmin_name())
                .withClaim("admin_id",admin.getAdmin_id())
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE))
                .sign(Algorithm.HMAC256(SECRET));
    }

    public static DecodedJWT verify(String token) {
        return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
    }

}
